package com.nmgs.util;

import sun.misc.BASE64Encoder;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class TomcatHTMLUtil {
    public static String message(String operateURL) {
        StringBuffer dataResult = new StringBuffer();
        URL url = null;
        try {
            url = new URL(operateURL);
            URLConnection conn = url.openConnection();
            String configuration = Dom4jReadXml.scriptUser+":"+Dom4jReadXml.scriptPass; // manager中script角色的用户
            String encodedPassword = new BASE64Encoder().encode(configuration.getBytes());
            conn.setRequestProperty("Authorization", "Basic " + encodedPassword);
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            conn.setRequestProperty("contentType", "UTF-8");
            // URL授权访问 -- End
            InputStream is = conn.getInputStream();
            BufferedReader bufreader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;
            while ((line = bufreader.readLine()) != null) {
                dataResult.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataResult.toString();
    }
}